﻿namespace MicroCloud.Log4Net
{
    /// <summary>
    /// Log4Net日志帮助类
    /// </summary>
    public sealed class Log4NetConfig
    {
        /// <summary>  
        /// 无参私有构造函数  
        /// </summary>  
        private Log4NetConfig() { }

        #region 属性
        #region 获取或设置 Log4Net日志config配置文件路径
        private static string _ConfigPath = string.Empty;
        /// <summary>
        /// 获取或设置 Log4Net日志config配置文件路径
        /// </summary>
        public static string ConfigPath
        {
            get
            {
                if (string.IsNullOrEmpty(_ConfigPath))
                {
                    _ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PathConstants.ConfigsPath);
                }
                return _ConfigPath;
            }
            set
            {
                _ConfigPath = value;
            }
        }
        #endregion

        #region 获取或设置 Log4Net日志config配置文件名称
        private static string _ConfigName = string.Empty;
        public static string ConfigName
        {
            get
            {
                if (string.IsNullOrEmpty(_ConfigName))
                {
                    _ConfigName = Log4NetConstants.Log4NetConfigName;
                }
                return _ConfigName;
            }
            set { _ConfigName = value; }
        }
        #endregion

        /// <summary>
        /// 获取 Log4Net日志config配置文件完整名称(含路径)
        /// </summary>
        public static string ConfigFullName
        {
            get
            {
                return Path.Combine(ConfigPath, ConfigName);
            }
        }

        /// <summary>
        /// 获取 程序集内配置文件资源完整路径
        /// </summary>
        public static string ConfigAssemblyPath
        {
            get
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    return string.Format("{0}.{1}.{2}", Log4NetConstants.Log4NetConfigPath, "Windows", Log4NetConstants.Log4NetConfigName);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    return string.Format("{0}.{1}.{2}", Log4NetConstants.Log4NetConfigPath, "Linux", Log4NetConstants.Log4NetConfigName);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    return string.Format("{0}.{1}.{2}", Log4NetConstants.Log4NetConfigPath, "OSX", Log4NetConstants.Log4NetConfigName);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
                {
                    return string.Format("{0}.{1}.{2}", Log4NetConstants.Log4NetConfigPath, "FreeBSD", Log4NetConstants.Log4NetConfigName);
                }
                else
                {
                    return string.Format("{0}.{1}", Log4NetConstants.Log4NetConfigPath, Log4NetConstants.Log4NetConfigName);
                }

            }
        }

        #endregion

        /// <summary>
        /// 初始化Log4Net日志config配置
        /// </summary>
        /// <param name="loggerRepository"></param>
        /// <returns></returns>
        public static bool InitLog4NetConfig(ILoggerRepository loggerRepository = null)
        {
            try
            {
                if (loggerRepository == null)
                {
                    Assembly assembly = Assembly.GetEntryAssembly() ?? Log4NetLoggerProvider.GetCallingAssemblyFromStartup();
                    loggerRepository = LogManager.CreateRepository(assembly, typeof(Hierarchy));
                }

                //Log4Net从log4net.config文件中读取配置信息
                XmlDocument confXmlDoc = new();
                var configStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigAssemblyPath);
                if (configStream == null)
                {
                    return false;
                }
                if (File.Exists(ConfigFullName))
                {
                    try
                    {
                        confXmlDoc.Load(File.OpenRead(ConfigFullName));
                    }
                    catch
                    {
                        if (File.Exists(ConfigFullName))
                        {
                            File.Delete(ConfigFullName);
                        }
                        confXmlDoc.Load(configStream);
                    }
                }
                else
                {
                    confXmlDoc.Load(configStream);
                }
                if (!Directory.Exists(ConfigPath))
                {
                    Directory.CreateDirectory(ConfigPath);
                }
                if (!File.Exists(ConfigFullName))
                {
                    confXmlDoc.Save(ConfigFullName);
                }
                XmlConfigurator.ConfigureAndWatch(loggerRepository, new FileInfo(ConfigFullName));

                return true;
            }
            catch { return false; }
        }

    }

}
